package com.example.crm.controller;

import com.example.crm.entity.WorkOrder;
import com.example.crm.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/workorders")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    @GetMapping
    public List<WorkOrder> getAllWorkOrders() {//查看所有
        return workOrderService.findAll();
    }

    @GetMapping("/{id}")
    public Optional<WorkOrder> getWorkOrderById(@PathVariable Integer id) {//根据id查找
        return workOrderService.findById(id);
    }

    @PostMapping
    public WorkOrder createWorkOrder(@RequestBody WorkOrder workOrder) {//新建
        return workOrderService.save(workOrder);
    }

    @PutMapping("/{id}")
    public WorkOrder updateWorkOrder(@PathVariable Integer id, @RequestBody WorkOrder workOrder) {//根据id更新
        workOrder.setOrderId(id);
        return workOrderService.save(workOrder);
    }

    @DeleteMapping("/{id}")
    public void deleteWorkOrder(@PathVariable Integer id) {
        workOrderService.deleteById(id);
    }
}
